app.controller('pv7', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    
	//附件
    $scope.pv9 = function () {
//        if($scope.ocrUsageInfoPage.length == 0){
//            $rootScope.tipPopups.show(($rootScope.tipMessage = "用量信息页信息需扫描"));
//            return;
//        }
//
//        for(var i=0;i<3;i++){
//            var obj = $scope.ocrUsageInfoPage[i];
//            if(!obj.totalColor){
//                $rootScope.tipPopups.show(($rootScope.tipMessage = "用量信息页信息需扫描"));
//                return;
//            }
//        }
        //判断数字
//        for(var i=0;i<3;i++){
//            var obj = $scope.ocrUsageInfoPage[i];
//            if(!isNumber(obj.other) || !isNumber(obj.monochrome) || !isNumber(obj.accent)
//                || !isNumber(obj.generalOffice) || !isNumber(obj.totalMono) || !isNumber(obj.totalColor)){
//                $rootScope.tipPopups.show(($rootScope.tipMessage = "请更正不为数字的值"));
//                return;
//            }
//        }

        /*if(!$scope.blh.long || !$scope.blh.lat){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "请开启定位稍后再试！"));
            return;
        }*/

       for(var i=0;i<3;i++){
           var obj = $scope.ocrUsageInfoPage[i];
           if(!obj.other){
               obj.other = "0";
           }if(!obj.monochrome){
               obj.monochrome = "0";
           }if(!obj.accent){
               obj.accent = "0";
           }if(!obj.generalOffice){
               obj.generalOffice = "0";
           }if(!obj.totalMono){
               obj.totalMono = "0";
           }if(!obj.totalColor){
               obj.totalColor = "0";
           }
           obj.longitude = $scope.blh.long;
           obj.latitude = $scope.blh.lat;
       }
        pv9.params.caseId = $scope.id;
        pv9.params.customeBlhLong = $scope.customerLng;
        pv9.params.customeBlhLat = $scope.customerLat;

        if($scope.ocrclickcount < 1){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "请点击用量信息页ocr识别进行扫描"));
            return;
        }

        localStorage.setItem("pv-caseId",$scope.id);
        localStorage.setItem("pv-ocrUsageInfoPage",JSON.stringify($scope.ocrUsageInfoPage));
        $state.go(pv9.stateName);
    }

    $scope.updatePopups = new ShowPopups('.update-popups');

    $scope.updateValue = function (value,msg) {
        var temp = '<input type="text" ng-model="'+value+'"/>';
        $("#temp").html('');
        $($compile(temp)($scope)).appendTo($("#temp"));
        $(".popups-content").addClass("new-popups-content");
        $(".title").addClass("new-title");
        $scope.confirmMessage = msg;
        $scope.updatePopups.show();
    }

    $scope.popupsCancel = function () {
        $(".popups-content").removeClass("new-popups-content");
        $(".title").removeClass("new-title");
        $scope.updatePopups.close();
    }
    //获得数据
    $scope.getOrders = function () {

    }

    //根据地址获得经纬度
    function geocoderGetLocation() {
        var address= $scope.pvAddress;
        AMap.plugin('AMap.Geocoder', function() {
            var geocoder = new AMap.Geocoder({
                // city 指定进行编码查询的城市，支持传入城市名、adcode 和 citycode
                // city: '重庆',
            });
            // 使用geocoder做地理/逆地理编码
            geocoder.getLocation(`${address}`, function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    // result中对应详细地理坐标信息
                    var lat=result.geocodes[0].location.lat;
                    var lng=result.geocodes[0].location.lng;
                    $scope.customerLng = lng;
                    $scope.customerLat = lat;
                    console.log(lat,lng);
                }
            })
        })
    }
    $scope.blhResult = function (msg) {
        //alert(JSON.stringify(msg));
        if(msg){
            var location = "";
            if(checkPhone('android')){
                location = JSON.parse(msg);
            }else if(checkPhone("iphone")){
                location = msg;
            }
            if(!location.long || !location.lat){
                $rootScope.tipPopups.show(($rootScope.tipMessage = "请开启定位稍后再试！"));
                // window.location.href = "index.html";
                return;
            }
            $scope.blh = location;
        }else{
            $rootScope.tipPopups.show(($rootScope.tipMessage = "请开启定位稍后再试！"));
            return;
        }
        // $scope.blh = JSON.parse(msg);
        // alert("-----"+JSON.stringify($scope.blh));
    }

    //检查gps是否打开
    $scope.checkGPS = function(){
        var flag = false;
        if(checkPhone('android')){
            flag = AndroidMethod.checkGps();
        }else if(checkPhone("iphone")){
            //flag = context.checkGps();
        }
        return flag;
    }

    //获得经纬度进行判断
    $scope.checkBlh = function (type) {
       //获得经纬度进行判断
       if(checkPhone('android')){
           var flag = $scope.checkGPS();
           if(!flag){
               $rootScope.tipPopups.show(($rootScope.tipMessage = "请开启定位稍后再试！"));
               return;
           }
           AndroidMethod.appBlh("pvBlhResult");
       }else if(checkPhone("iphone")){
		   window.webkit.messageHandlers.appBlh.postMessage("pvBlhResult");
       }
       if(!$scope.blh){
           $rootScope.tipPopups.show(($rootScope.tipMessage = "暂未获取当前定位地址，请稍后再试！"));
           return;
       }

      /* var reqeustData = {
           "caseId": $scope.id,
           "longitude": $scope.blh.long,
           "latitude": $scope.blh.lat,
           "customerLongitude":$scope.customerLng,
           "customerLatitude":$scope.customerLat
       };
       postHttp($http, 'pvCase/checkGPS', reqeustData, function (res) {
           if (res.msg === 'success') {
               //$scope.runScan(type);
           } else {
              // $rootScope.tipPopups.show(($rootScope.tipMessage = res.msg));
               //return;
           }
       });*/
       $scope.runScan(type);
    }
    //执行app扫描
    $scope.runScan = function (type) {
        //执行ocr扫描
        $scope.type = type;
        var scantype = "pvOcrResult";
        if(checkPhone('android')){
            AndroidMethod.ocr(getToken(),scantype);
        }else if(checkPhone("iphone")){
			window.webkit.messageHandlers.ocr.postMessage({"token": getToken(),"scanType":scanType});
        }else{
			 hm.ocr(getToken(),scantype);
		}
    }



    //扫描
    $scope.scanOCR = function (type) {
        $scope.ocrclickcount += 1;  //记录ocr识别按钮点击次数

        $scope.checkBlh(type);
    }

    function handleData(value,data) {
        if(!data || !data[0]){
            data = [];
            for(var i=0;i<3;i++){
                data[i] = "";
            }
        }
        for(var i=0;i<3;i++){
            if(!$scope.ocrUsageInfoPage[i]){
                $scope.ocrUsageInfoPage[i] = {};
            }
            if(value == "type"){
                $scope.ocrUsageInfoPage[i][value] = (i+1);
                continue;
            }
            var tempData = data[i];
            if(tempData && (tempData.indexOf(",") > -1)){
                tempData = tempData.replace(",","");
            }
            $scope.ocrUsageInfoPage[i][value] = tempData;

//            if($scope.blh.long && $scope.blh.lat){
//                $scope.ocrUsageInfoPage[i].longitude = $scope.blh.long;
//                $scope.ocrUsageInfoPage[i].latitude = $scope.blh.lat;
//            }else{
//                $scope.ocrUsageInfoPage[i].longitude = "109.09";
//                $scope.ocrUsageInfoPage[i].latitude = "109.09";
//            }
//
//            // $scope.ocrUsageInfoPage[i].longitude = "109.09";
//            // $scope.ocrUsageInfoPage[i].latitude = "109.09";
//
//            if(value == "other"){
//                if(!$scope.ocrUsageInfoPage[i].other){
//                    $scope.ocrUsageInfoPage[i].other = 0;
//                }
//            }
        }
        $scope.$applyAsync();
    }
    
    function supportModel(msg){
    	//getOCRValue(msg, $scope.scanModel, '用量页-黑白');
    	//return typeModel;
		return getOCRTypeModel($scope.scanModel);
    }

    function rehanldeData() {
        for(var i=0;i<3;i++){
            var obj = $scope.ocrUsageInfoPage[i];
            if(obj.monochrome){
                obj.totalMono = "0";
            }else if($scope.pvcolor[i]){
                obj.totalColor = "0";
            }
        }
    }
    
    //扫描结果
    $scope.scanResult = function (msg) {
    	var username = sessionStorage.getItem("username");
    	
    	var ocrtype = supportModel(msg);
    	
		if(3==$scope.type || "3" == $scope.type){//用量信息页
            var monochrome = getOCRValue(msg, $scope.scanModel, '用量页-黑白');
            var monochromeAry = monochrome.split(";");
            handleData("monochrome",monochromeAry);
            var accent = getOCRValue(msg, $scope.scanModel, '用量页-强化');
            var accentAry = accent.split(";");
            handleData("accent",accentAry);
            var generalOffice = getOCRValue(msg, $scope.scanModel, '用量页-一般办公');
            var generalOfficeAry = generalOffice.split(";");
            handleData("generalOffice",generalOfficeAry);
            var totalMono = getOCRValue(msg, $scope.scanModel, '用量页-总数');
            var totalMonoAry = totalMono.split(";");
            handleData("totalMono",totalMonoAry);
            var totalColor = getOCRValue(msg, $scope.scanModel, '用量页-色彩总数');
            var totalColorAry = totalColor.split(";");
            handleData("totalColor",totalColorAry);
            var other = getOCRValue(msg, $scope.scanModel, '用量页-其它');
            var otherAry = other.split(";");
            handleData("other",otherAry);
            handleData("type");


            //新增总数和彩色
            var pvcolor = getOCRValue(msg, $scope.scanModel, '用量页-彩色');
            $scope.pvcolor = pvcolor.split(";");
            var pvtotal = getOCRValue(msg, $scope.scanModel, '用量页-总数');
            $scope.pvtotal = pvtotal.split(";");
            
            //$('#debugDivInfo').val(JSON.stringify($scope.ocrUsageInfoPage));
            
			/*if(username == 'yuan.hao.huang@hp.com'){
				$('#debugDivInfo').val(ocrtype+":"+JSON.stringify(msg));
				$('#debugDiv').show();
				$('#debugDivInfo').show();
			}*/
			
            rehanldeData();
            switch(ocrtype){
            case 1:
        		$scope.ocrUsageInfoPage[0].monochrome = 0;
        		$scope.ocrUsageInfoPage[0].accent = 0;
        		$scope.ocrUsageInfoPage[0].generalOffice = 0;
        		$scope.ocrUsageInfoPage[0].totalColor = 0;
        		$scope.ocrUsageInfoPage[0].other = 0;
        		$scope.ocrUsageInfoPage[0].totalMono=$scope.pvtotal[0];
        		$scope.ocrUsageInfoPage[1].monochrome = 0;
        		$scope.ocrUsageInfoPage[1].accent = 0;
        		$scope.ocrUsageInfoPage[1].generalOffice = 0;
        		$scope.ocrUsageInfoPage[1].totalMono = 0;
        		$scope.ocrUsageInfoPage[1].totalColor = 0;
        		$scope.ocrUsageInfoPage[1].other = 0;
        		$scope.ocrUsageInfoPage[2].monochrome = 0;
        		$scope.ocrUsageInfoPage[2].accent = 0;
        		$scope.ocrUsageInfoPage[2].generalOffice = 0;
        		$scope.ocrUsageInfoPage[2].totalMono = 0;
        		$scope.ocrUsageInfoPage[2].totalColor = 0;
        		$scope.ocrUsageInfoPage[2].other = 0;
        		break;
            case 2:
            	/*$scope.ocrUsageInfoPage[0].totalMono = $scope.ocrUsageInfoPage[0].monochrome;
        		$scope.ocrUsageInfoPage[0].totalColor = $scope.pvcolor[0];
            	$scope.ocrUsageInfoPage[0].monochrome = 0;
        		$scope.ocrUsageInfoPage[0].accent = 0;
        		$scope.ocrUsageInfoPage[0].generalOffice = 0;
        		$scope.ocrUsageInfoPage[0].other = 0;
        		$scope.ocrUsageInfoPage[1].monochrome = 0;
        		$scope.ocrUsageInfoPage[1].accent = 0;
        		$scope.ocrUsageInfoPage[1].generalOffice = 0;
        		$scope.ocrUsageInfoPage[1].totalMono = 0;
        		$scope.ocrUsageInfoPage[1].totalColor = 0;
        		$scope.ocrUsageInfoPage[1].other = 0;
        		$scope.ocrUsageInfoPage[2].monochrome = 0;
        		$scope.ocrUsageInfoPage[2].accent = 0;
        		$scope.ocrUsageInfoPage[2].generalOffice = 0;
        		$scope.ocrUsageInfoPage[2].totalMono = 0;
        		$scope.ocrUsageInfoPage[2].totalColor = 0;
        		$scope.ocrUsageInfoPage[2].other = 0;*/
				$scope.ocrUsageInfoPage[0].totalMono = $scope.ocrUsageInfoPage[0].monochrome;
        		$scope.ocrUsageInfoPage[0].totalColor = $scope.pvcolor[0];
            	$scope.ocrUsageInfoPage[0].monochrome = 0;
        		$scope.ocrUsageInfoPage[0].accent = 0;
        		$scope.ocrUsageInfoPage[0].generalOffice = 0;
        		$scope.ocrUsageInfoPage[0].other = 0;
				if($scope.ocrUsageInfoPage[1].monochrome){
					$scope.ocrUsageInfoPage[1].totalMono = $scope.ocrUsageInfoPage[1].monochrome;
				}else{
					$scope.ocrUsageInfoPage[1].totalMono = 0;
				}
				if($scope.pvcolor[1]){
					$scope.ocrUsageInfoPage[1].totalColor = $scope.pvcolor[1];
				}else{
					$scope.ocrUsageInfoPage[1].totalColor = 0;
				}
        		$scope.ocrUsageInfoPage[1].monochrome = 0;
        		$scope.ocrUsageInfoPage[1].accent = 0;
        		$scope.ocrUsageInfoPage[1].generalOffice = 0;
        		$scope.ocrUsageInfoPage[1].other = 0;
				if($scope.ocrUsageInfoPage[2].monochrome){
					$scope.ocrUsageInfoPage[2].totalMono = $scope.ocrUsageInfoPage[2].monochrome;
				}else{
					$scope.ocrUsageInfoPage[2].totalMono = 0;
				}
				if($scope.pvcolor[2]){
					$scope.ocrUsageInfoPage[2].totalColor = $scope.pvcolor[2];
				}else{
					$scope.ocrUsageInfoPage[2].totalColor = 0;
				}
        		$scope.ocrUsageInfoPage[2].monochrome = 0;
        		$scope.ocrUsageInfoPage[2].accent = 0;
        		$scope.ocrUsageInfoPage[2].generalOffice = 0;
        		$scope.ocrUsageInfoPage[2].other = 0;
        		break;
            case 3:
        		$scope.ocrUsageInfoPage[0].totalMono = 0;
        		$scope.ocrUsageInfoPage[0].totalColor = 0;
        		$scope.ocrUsageInfoPage[1].monochrome = 0;
        		$scope.ocrUsageInfoPage[1].accent = 0;
        		$scope.ocrUsageInfoPage[1].generalOffice = 0;
        		$scope.ocrUsageInfoPage[1].totalMono = 0;
        		$scope.ocrUsageInfoPage[1].totalColor = 0;
        		$scope.ocrUsageInfoPage[1].other = 0;
        		$scope.ocrUsageInfoPage[2].monochrome = 0;
        		$scope.ocrUsageInfoPage[2].accent = 0;
        		$scope.ocrUsageInfoPage[2].generalOffice = 0;
        		$scope.ocrUsageInfoPage[2].totalMono = 0;
        		$scope.ocrUsageInfoPage[2].totalColor = 0;
        		$scope.ocrUsageInfoPage[2].other = 0;
        		break;
            case 4:
            	$scope.ocrUsageInfoPage[0].monochrome = 0;
        		$scope.ocrUsageInfoPage[0].accent = 0;
        		$scope.ocrUsageInfoPage[0].generalOffice = 0;
        		$scope.ocrUsageInfoPage[0].totalColor = 0;
        		$scope.ocrUsageInfoPage[0].other = 0;
        		$scope.ocrUsageInfoPage[0].totalMono=$scope.pvtotal[0];
        		$scope.ocrUsageInfoPage[1].monochrome = 0;
        		$scope.ocrUsageInfoPage[1].accent = 0;
        		$scope.ocrUsageInfoPage[1].generalOffice = 0;
        		$scope.ocrUsageInfoPage[1].totalMono=$scope.pvtotal[1];
        		$scope.ocrUsageInfoPage[1].totalColor = 0;
        		$scope.ocrUsageInfoPage[1].other = 0;
				$scope.ocrUsageInfoPage[2].monochrome = 0;
        		$scope.ocrUsageInfoPage[2].accent = 0;
        		$scope.ocrUsageInfoPage[2].generalOffice = 0;
        		$scope.ocrUsageInfoPage[2].totalMono = $scope.pvtotal[2];
        		$scope.ocrUsageInfoPage[2].totalColor = 0;
        		$scope.ocrUsageInfoPage[2].other = 0;
        		break;
            case 5:
            	$scope.ocrUsageInfoPage[0].totalMono = $scope.ocrUsageInfoPage[0].monochrome;
        		$scope.ocrUsageInfoPage[0].totalColor = $scope.pvcolor[0];
            	$scope.ocrUsageInfoPage[0].monochrome = 0;
        		$scope.ocrUsageInfoPage[0].accent = 0;
        		$scope.ocrUsageInfoPage[0].generalOffice = 0;
        		$scope.ocrUsageInfoPage[0].other = 0;
        		$scope.ocrUsageInfoPage[1].totalMono = $scope.ocrUsageInfoPage[1].monochrome;
        		$scope.ocrUsageInfoPage[1].totalColor = $scope.pvcolor[1];
        		$scope.ocrUsageInfoPage[1].monochrome = 0;
        		$scope.ocrUsageInfoPage[1].accent = 0;
        		$scope.ocrUsageInfoPage[1].generalOffice = 0;
        		$scope.ocrUsageInfoPage[1].other = 0;
        		$scope.ocrUsageInfoPage[2].totalMono = $scope.ocrUsageInfoPage[2].monochrome;
        		$scope.ocrUsageInfoPage[2].totalColor = $scope.pvcolor[2];
        		$scope.ocrUsageInfoPage[2].monochrome = 0;
        		$scope.ocrUsageInfoPage[2].accent = 0;
        		$scope.ocrUsageInfoPage[2].generalOffice = 0;
        		$scope.ocrUsageInfoPage[2].other = 0;
        		break;
            case 6:
        		$scope.ocrUsageInfoPage[0].totalMono = 0;
        		$scope.ocrUsageInfoPage[0].totalColor = 0;
        		$scope.ocrUsageInfoPage[1].totalMono = 0;
        		$scope.ocrUsageInfoPage[1].totalColor = 0;
        		$scope.ocrUsageInfoPage[2].totalMono = 0;
        		$scope.ocrUsageInfoPage[2].totalColor = 0;
        		break;
        	}

            $scope.$applyAsync();
            localStorage.setItem("pv-ocrUsageInfoPage",JSON.stringify($scope.ocrUsageInfoPage));
       }
    }
    
//    var data99740PV = {"log_id":5711858868687571354,"words_result_num":19,"words_result":[{"words":"等效印数(Letter\/A4)"},{"words":"彩色"},{"words":"黑白强化一般办公"},{"words":"其它色彩总数"},{"words":"总数"},{"words":"打印5.808.4650.0"},{"words":"0.02,177.62,827.68,636.0"},{"words":"复印"},{"words":"67.00.0"},{"words":"0.0"},{"words":"38.0"},{"words":"38.0"},{"words":"105.0"},{"words":"传真"},{"words":"0.00.0"},{"words":"0.0"},{"words":"0.0"},{"words":"0.0"},{"words":"0.0"}]}
//
//    $scope.ocrUsageInfoPage = [];
//    $scope.ocrUsageInfoPage[0] = 0;
//    $scope.ocrUsageInfoPage[1] = 0;
//    $scope.ocrUsageInfoPage[2] = 0;
//    $scope.scanModel = "77940";//, '用量页-黑白'
//    $scope.type = 3;
//    $scope.scanResult(data99740PV);
//    //alert(getOCRValue(dataM605, 'M605', '用量页-总数'));


    //初始化
    function init(){
        //定位
        $scope.blh = {
            long:"",
            lat:""
        };
        //ocr点击次数
        $scope.ocrclickcount = 0;

        $scope.ocrUsageInfoPage1 = JSON.parse(localStorage.getItem("pv-lastOcrUsageInfoPage"));
        $scope.ocrUsageInfoPage = [];

        $scope.productName = pv7.params.productName;

        $scope.scanModel = $scope.productName;   //扫描型号
        if(localStorage.getItem("pv-ocrUsageInfoPage")){
            $scope.ocrUsageInfoPage = JSON.parse(localStorage.getItem("pv-ocrUsageInfoPage"));
        }
        //案例id
        $scope.id = pv7.params.caseId;
        if(!pv7.params.caseId){
            $scope.id = localStorage.getItem("pv-caseId");
        }
        $scope.pvAddress = pv7.params.address;
        geocoderGetLocation()
       /* var data2 = {
            "log_id" : 3650753902062636342,
            "words_result" : [ {
                "words" : "等效印数(Letter\/A4)"
            }, {
                "words" : "彩色"
            }, {
                "words" : "黑白强化一般办公其它色彩总数总数"
            }, {
                "words" : "打印"
            }, {
                "words" : "0.0"
            }, {
                "words" : "0.0"
            }, {
                "words" : "0.00.0"
            }, {
                "words" : "0.00.0"
            }, {
                "words" : "复印"
            }, {
                "words" : "0.0"
            }, {
                "words" : "0.0"
            }, {
                "words" : "0.00.0"
            }, {
                "words" : "0.00.0"
            }, {
                "words" : "传真"
            }, {
                "words" : "0.0"
            }, {
                "words" : "*"
            }, {
                "words" : "0.0"
            }, {
                "words" : "0.00.0"
            }, {
                "words" : "0.00.0"
            } ],
            "words_result_num" : 19
        }

        $scope.scanResult(data2,3);*/
    }

    init();


    reloadJs();

});
function ocrResult(msg){
	// alert(msg);
}
//返回扫描结果
function pvOcrResult(msg) {
    var $scope = getScope("pv7");
    if(checkPhone('android')){
        msg = JSON.parse(msg);
    }
//alert(msg);
    // debugger
    $scope.scanResult(msg);
}
function pvBlhResult(msg) {
    var $scope = getScope("pv7");
    if(!$scope){
        $scope = getScope("az13");
        sdmBlhResult(msg);
    }else{
        $scope.blhResult(msg);
    }
}